package com.xinghai.system.service.impl;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.xinghai.common.utils.DateUtils;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.system.mapper.QdShopGoodMapper;
import com.xinghai.system.domain.QdShopGood;
import com.xinghai.system.service.IQdShopGoodService;

/**
 * 店铺商品Service业务层处理
 *
 * @author xinghai
 * @date 2025-01-26
 */
@Service
public class QdShopGoodServiceImpl extends ServiceImpl<QdShopGoodMapper, QdShopGood>
        implements IQdShopGoodService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询店铺商品列表
     *
     * @param qdShopGood 店铺商品
     * @return 店铺商品集合
     */
    @Override
    public List<QdShopGood> list(QdShopGood qdShopGood) {
        QueryWrapper<QdShopGood> queryWrapper = new QueryWrapper<>();
        if (qdShopGood.getStatus()!= null  && !qdShopGood.getStatus().isEmpty()){
            queryWrapper.eq("status", qdShopGood.getStatus());
        }
        if (qdShopGood.getStoreName()!= null  && !qdShopGood.getStoreName().isEmpty()){
            queryWrapper.like("store_name", qdShopGood.getStoreName());
        }
        if (qdShopGood.getStoreId()!= null){
            queryWrapper.like("store_id", qdShopGood.getStoreId());
        }
        if (qdShopGood.getGoodName()!= null  && !qdShopGood.getGoodName().isEmpty()){
            queryWrapper.like("good_name", qdShopGood.getGoodName());
        }
        qdShopGood.setDelFlag(GenConstants.DELETE_FLAG_NO);
        return this.list(queryWrapper);
    }

    /**
     * 删除店铺商品信息
     * @param id 需要删除的数据ID
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    public boolean delete(Long id, boolean flag) {
        if (flag){
            QdShopGood qdShopGood = new QdShopGood();
            qdShopGood.setId(id);
            qdShopGood.setDelFlag(GenConstants.DELETE_FLAG_YES);
            return this.updateById(qdShopGood);
        }
        return this.removeById(id);
    }

    /**
     * 批量删除店铺商品信息
     *
     * @param ids 需要删除的数据ID集合
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteBatch(Long[] ids, boolean flag) {
        if (flag){
            List<QdShopGood> collect = Arrays.stream(ids).map(m -> {
                QdShopGood qdShopGood = new QdShopGood();
                qdShopGood.setId(m);
                qdShopGood.setDelFlag(GenConstants.DELETE_FLAG_YES);
                return qdShopGood;
            }).collect(Collectors.toList());
            return this.updateBatchById(collect);
        }
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 导入店铺商品信息
     *
     * @param list 店铺商品信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<QdShopGood> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            QdShopGoodMapper qdShopGoodMapper = sqlSession.getMapper(QdShopGoodMapper.class);
            list.forEach(qdShopGoodMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
